#import "BuglyPlugin.h"
#import <Bugly/Bugly.h>

@implementation BuglyPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"bugly_plugin"
            binaryMessenger:[registrar messenger]];
  BuglyPlugin* instance = [[BuglyPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"getPlatformVersion" isEqualToString:call.method]) {
    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  } else if([@"postDartError" isEqualToString:call.method]) {
      NSDictionary* exceptionMap = call.arguments;
      NSString* exceptionName = @"dartError";
      NSString* exceptionReason = exceptionMap[@"exception"];
      NSDictionary* exceptionUserInfo = nil;
      NSArray* array = exceptionMap[@"stack"];
      for (NSString * value in array) {
          [BuglyLog level:BuglyLogLevelError logs:value];
      }
      NSException* exception = [NSException exceptionWithName:exceptionName reason:exceptionReason userInfo:exceptionUserInfo];
    
      [Bugly reportException:exception];
  } else if([@"postTestError" isEqualToString:call.method]) {
      NSString* value;
      NSInteger size = [value length];
  } else {
    result(FlutterMethodNotImplemented);
  }
}

@end
